<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="【cURL】CURLOPT_RETURNTRANSFER选项使用实验详解"/>




  <meta name="keywords" content="cURL, 小明的笔记" />










  <link rel="alternate" href="/atom.xml" title="小明的笔记">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.9.0" />



<link rel="canonical" href="https://veris.gitee.io/2017/08/02/【cURL】CURLOPT-RETURNTRANSFER选项使用实验详解/"/>



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  <link rel="stylesheet" type="text/css" href="/lib/nprogress/nprogress.min.css" />



<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.9.0" />



  



  <script id="baidu_push">
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>








<script>
  window.config = {"title":"小明的笔记","subtitle":"天道酬勤","description":null,"author":"Veris","language":"zh-CN","timezone":"Asia/Shanghai","url":"https://veris.gitee.io","root":"/","permalink":":year/:month/:day/:title/","permalink_defaults":null,"source_dir":"source","public_dir":"public","tag_dir":"tags","archive_dir":"archives","category_dir":"categories","code_dir":"downloads/code","i18n_dir":":lang","skip_render":null,"new_post_name":":title.md","default_layout":"post","titlecase":false,"external_link":true,"filename_case":0,"render_drafts":false,"post_asset_folder":true,"relative_link":false,"future":true,"highlight":{"enable":true,"auto_detect":false,"line_number":true,"tab_replace":null},"default_category":"uncategorized","category_map":null,"tag_map":null,"date_format":"YYYY-MM-DD","time_format":"HH:mm:ss","per_page":10,"pagination_dir":"page","theme":"hexo-theme-even","deploy":{"type":"git","repo":"https://gitee.com/Veris/Veris.git","breanch":"master"},"ignore":[],"index_generator":{"per_page":10,"order_by":"-date","path":""},"archive_generator":{"per_page":10,"yearly":true,"monthly":true,"daily":false},"category_generator":{"per_page":10},"feed":{"type":"atom","limit":20,"hub":"","content":true,"content_limit":140,"content_limit_delim":"","path":"atom.xml"},"tag_generator":{"per_page":10},"marked":{"gfm":true,"pedantic":false,"sanitize":false,"tables":true,"breaks":true,"smartLists":true,"smartypants":true,"modifyAnchors":"","autolink":true},"server":{"port":4000,"log":false,"ip":"0.0.0.0","compress":false,"header":true},"since":2015,"favicon":"/favicon.ico","rss":"default","menu":{"Home":"/","Archives":"/archives/","Tags":"/tags/","Categories":"/categories/","About":"/about/"},"color":"Default","toc":true,"fancybox":true,"pjax":true,"copyright":{"enable":true,"license":"<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-nc/4.0/\" target=\"_blank\">知识共享署名-非商业性使用 4.0 国际许可协议</a>"},"reward":{"enable":false,"qrCode":{"wechat":null,"alipay":null}},"social":{"email":"562854011@qq.com","stack-overflow":null,"twitter":null,"facebook":null,"linkedin":null,"google":null,"github":"https://github.com/VerisFung","weibo":"https://weibo.com/onnfzstz69","zhihu":null,"douban":null,"pocket":null,"tumblr":null,"instagram":null},"leancloud":{"app_id":null,"app_key":null},"baidu_analytics":null,"baidu_verification":null,"google_analytics":null,"google_verification":null,"disqus_shortname":null,"changyan":{"appid":null,"appkey":null},"livere_datauid":null,"version":"2.9.0"};
</script>

    <title> 【cURL】CURLOPT_RETURNTRANSFER选项使用实验详解 - 小明的笔记 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">小明的笔记</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/">
        <li class="mobile-menu-item">
          
          
            首页
          
        </li>
      </a>
    
      <a href="/archives/">
        <li class="mobile-menu-item">
          
          
            归档
          
        </li>
      </a>
    
      <a href="/tags/">
        <li class="mobile-menu-item">
          
          
            标签
          
        </li>
      </a>
    
      <a href="/categories/">
        <li class="mobile-menu-item">
          
          
            分类
          
        </li>
      </a>
    
      <a href="/about/">
        <li class="mobile-menu-item">
          
          
            关于
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">小明的笔记</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/">
            
            
              首页
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives/">
            
            
              归档
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags/">
            
            
              标签
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/categories/">
            
            
              分类
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about/">
            
            
              关于
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          【cURL】CURLOPT_RETURNTRANSFER选项使用实验详解
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2017-08-02
        </span>
        
          <div class="post-category">
            
              <a href="/categories/PHP/">PHP</a>
            
          </div>
        
        
      </div>
    </header>

    
    


    <div class="post-content">
      
        <p><code>curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);</code></p>
<p>参数值：</p>
<ul>
<li>false： 关闭返回，直接输出内容</li>
<li>true： 开启返回，不直接输出内容</li>
</ul>
<blockquote>
<p>开启后使用<code>curl_exec</code>函数或<code>curl_multi_getcontent</code>函数返回网站内容，两个函数的返回区别如下：</p>
</blockquote>
<ul>
<li><p><strong>使用</strong><code>curl_exec</code><strong>函数</strong></p>
  <figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">    $ch=curl_init();</div><div class="line">    curl_setopt($ch,CURLOPT_URL,<span class="string">'http://www.mostclan.com/'</span>);</div><div class="line">    curl_setopt($ch,CURLOPT_HEADER,<span class="number">0</span>);</div><div class="line">    $a=curl_exec($ch);</div><div class="line">    curl_close($ch);</div><div class="line">    var_dump($a);</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>  执行结果：</p>
<ul>
<li>直接输出了网页内容</li>
<li>curl_exec函数返回<code>boolean true</code><a id="more"></a></li>
</ul>
</li>
<li><p><strong>使用</strong><code>curl_multi_getcontent</code><strong>函数</strong></p>
  <figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">    $ch=curl_init();</div><div class="line">    curl_setopt($ch,CURLOPT_URL,<span class="string">'http://www.mostclan.com/'</span>);</div><div class="line">    curl_setopt($ch,CURLOPT_HEADER,<span class="number">0</span>);</div><div class="line">    curl_setopt($ch,CURLOPT_RETURNTRANSFER,<span class="number">1</span>);</div><div class="line">    $a=curl_exec($ch);</div><div class="line">    $b=curl_multi_getcontent($ch);</div><div class="line">    curl_close($ch);</div><div class="line">    var_dump($a);</div><div class="line">    var_dump($b);</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<ul>
<li><p>设置<code>CURLOPT_RETURNTRANSFER</code>为<code>true</code>结果：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">string '<span class="meta">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">xmlns</span>=<span class="string">"http://www.w3.org/1999/xhtml"</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">"Content-Type"</span> <span class="attr">content</span>=<span class="string">"text/html; charset=UTF-8"</span> /&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">title</span>&gt;</span>最族 - 关注互联网技术的网络工作室<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"keywords"</span> <span class="attr">content</span>=<span class="string">"最族,最族网络,最族游戏,最族软件,最族工作室,最族网络工作室,最族天下,最族传媒,最族科技,游戏制作,独立游戏开发,Mostclan,Veris,Game'... (length=59109)</span></span></div></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">string '<span class="meta">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">xmlns</span>=<span class="string">"http://www.w3.org/1999/xhtml"</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">"Content-Type"</span> <span class="attr">content</span>=<span class="string">"text/html; charset=UTF-8"</span> /&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">title</span>&gt;</span>最族 - 关注互联网技术的网络工作室<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"keywords"</span> <span class="attr">content</span>=<span class="string">"最族,最族网络,最族游戏,最族软件,最族工作室,最族网络工作室,最族天下,最族传媒,最族科技,游戏制作,独立游戏开发,Mostclan,Veris,Game'... (length=59109)</span></span></div></pre></td></tr></table></figure>
<p>返回了两个网页源代码</p>
</li>
<li><p>设置<code>CURLOPT_RETURNTRANSFER</code>为<code>false</code>结果：</p>
<ul>
<li>$a 返回 boolean true 并直接输出网页内容</li>
<li>$b 返回 null</li>
</ul>
</li>
</ul>
</li>
</ul>
<blockquote>
<p>总结：<code>CURLOPT_RETURNTRANSFER</code>值为<code>true</code>时则会终止<code>curl_exec</code>直接输出内容，且将内容已函数返回值的形式返回，可用<code>curl_exec</code>或<code>curl_multi_getcontent</code>获取返回值；不同的是当<code>CURLOPT_RETURNTRANSFER</code>值为<code>false</code>时，<code>curl_exec</code>值为<code>布尔值</code>，<code>curl_multi_getcontent</code>为<code>字符串</code>或<code>null</code>值。</p>
</blockquote>
<table>
<thead>
<tr>
<th style="text-align:left">CURLOPT_RETURNTRANSFER</th>
<th style="text-align:left">curl_exec()</th>
<th style="text-align:left">curl_multi_getcontent()</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">true 或 1</td>
<td style="text-align:left">终止输出页面内容，返回获取的文本流，无内容返回空串，若curl执行失败则返回false</td>
<td style="text-align:left">返回获取的文本流，无内容返回null</td>
</tr>
<tr>
<td style="text-align:left">false 或 0</td>
<td style="text-align:left">输出页面内容，返回cURL状态</td>
<td style="text-align:left">返回null</td>
</tr>
</tbody>
</table>

      
    </div>

    
      
      

  <div class="post-copyright">
    <p class="copyright-item">
      <span>原文作者: </span>
      <a href="https://veris.gitee.io">Veris</a>
    </p>
    <p class="copyright-item">
      <span>原文链接: </span>
      <a href="https://veris.gitee.io/2017/08/02/【cURL】CURLOPT-RETURNTRANSFER选项使用实验详解/">https://veris.gitee.io/2017/08/02/【cURL】CURLOPT-RETURNTRANSFER选项使用实验详解/</a>
    </p>
    <p class="copyright-item">
      <span>许可协议: </span>
      
      <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/cURL/">cURL</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/2017/08/02/【cURL】实例一、上传文件/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">【cURL】实例一、上传文件</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    
    
      <a class="next" href="/2017/08/02/【cURL】获取一个网页内容/">
        <span class="next-text nav-default">【cURL】cURL获取一个网页内容</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:562854011@qq.com" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
        
          <a href="https://github.com/VerisFung" class="iconfont icon-github" title="github"></a>
        
      
    
      
        
          <a href="https://weibo.com/onnfzstz69" class="iconfont icon-weibo" title="weibo"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
    
    
      <a href="/atom.xml" class="iconfont icon-rss" title="rss"></a>
    
  </div>


<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://hexo.io/">Hexo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>

  <span class="copyright-year">
    
    &copy; 
     
      2015 - 
    
    2018

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">Veris</span>
  </span>
</div>

      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  

  



    
  



  
  





  
    <script type="text/javascript" src="/lib/jquery/jquery.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  

  
    <script type="text/javascript" src="/lib/pjax/jquery.pjax.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/nprogress/nprogress.min.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.9.0"></script>

  </body>
</html>
